package com.shujia.wyh.day21;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
        HashMap<Integer,String>
 */
public class HashMapDemo2 {
    public static void main(String[] args) {
        //创建集合对象
        HashMap<Integer, String> map = new HashMap<>();

        //向集合中添加元素
        map.put(1001, "张三");
        map.put(1002, "李四");
        map.put(1003, "王五");
        map.put(1004, "张三");

        //遍历集合
        //方式1：根据键找值
        Set<Integer> keys = map.keySet();
        for (Integer key : keys) {
            String value = map.get(key);
            System.out.println(key + ":" + value);
        }
        System.out.println("==============================");
        //方式2：根据键值对找键和值
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer, String> keyValue : entries) {
            Integer key = keyValue.getKey();
            String value = keyValue.getValue();
            System.out.println(key + "---" + value);
        }


    }
}
